package com.haixiaoke.saas.repository.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.haixiaoke.saas.repository.mapper.OpHouseMapper;
import com.haixiaoke.saas.repository.pojo.ApartmentLabel;
import com.haixiaoke.saas.repository.pojo.HouseFacilities;
import com.haixiaoke.saas.repository.pojo.OpHouse;
import com.haixiaoke.saas.repository.service.IApartmentLabelService;
import com.haixiaoke.saas.repository.service.IHouseFacilitiesService;
import com.haixiaoke.saas.repository.service.IOpHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 公众号房源Service业务层处理
 *
 * @author qixi
 * @date 2024-01-16
 */
@Service
public class OpHouseServiceImpl implements IOpHouseService {
    @Autowired
    private OpHouseMapper opHouseMapper;
    @Autowired
    private IApartmentLabelService apartmentLabelService;
    @Autowired
    private IHouseFacilitiesService houseFacilitiesService;

    /**
     * 查询公众号房源
     *
     * @param id 公众号房源主键
     * @return 公众号房源
     */
    @Override
    public OpHouse selectOpHouseById(Long id) {
        OpHouse opHouse = opHouseMapper.selectOpHouseById(id);
        if (ObjectUtil.isNotNull(opHouse)) {
            completeInfo(opHouse);
        }
        return opHouse;
    }

    /**
     * 查询公众号房源列表
     *
     * @param opHouse 公众号房源
     * @return 公众号房源
     */
    @Override
    public List<OpHouse> selectOpHouseList(OpHouse opHouse) {
        List<OpHouse> opHouses = opHouseMapper.selectOpHouseList(opHouse);
        opHouses.forEach(
                m -> {
                    completeInfo(m);
                }
        );
        return opHouses;
    }


    /**
     * 补全信息(标签,房屋设施)
     *
     * @param opHouse
     */
    private void completeInfo(OpHouse opHouse) {
        if (StrUtil.isNotBlank(opHouse.getLabelIds())) {
            List<Long> labelIds = StrUtil.splitTrim(opHouse.getLabelIds(), ',').stream().map(Long::parseLong).collect(Collectors.toList());
            opHouse.setApartmentLabelList(apartmentLabelService.selectApartmentLabelByLabelIdIn(labelIds));
        }
        if (StrUtil.isNotBlank(opHouse.getFacilitiesIds())) {
            List<Long> facilitiesIds = StrUtil.splitTrim(opHouse.getFacilitiesIds(), ',').stream().map(Long::parseLong).collect(Collectors.toList());

            List<HouseFacilities> facilities = houseFacilitiesService.selectHouseFacilitiesList(null);

            // 遍历facilities集合，根据labelIds设置exist属性
            facilitiesIds.forEach(id -> {
                facilities.stream()
                        .filter(facility -> facility.getFacilitiesId().equals(id))
                        .forEach(facility -> facility.setExist(true));
            });

            // 根据exist属性和sort属性进行排序(exist=true的排在前面, sort越小越靠前)
            List<HouseFacilities> sortedFacilities = facilities.stream()
                    .sorted(Comparator.comparing(HouseFacilities::isExist).reversed()
                            .thenComparing(HouseFacilities::getSort))
                    .collect(Collectors.toList());

            opHouse.setFacilitiesList(sortedFacilities);
        }
    }

    /**
     * 新增公众号房源
     *
     * @param opHouse 公众号房源
     * @return 结果
     */
    @Override
    public int insertOpHouse(OpHouse opHouse) {
        opHouse.setCreateTime(new Date());

        List<HouseFacilities> facilitiesList = opHouse.getFacilitiesList();
        if (CollectionUtil.isNotEmpty(facilitiesList)) {
            String facilitiesIds = StrUtil.join(",", facilitiesList.stream().map(HouseFacilities::getFacilitiesId).collect(Collectors.toList()));
            opHouse.setFacilitiesIds(facilitiesIds);
        }

        List<ApartmentLabel> apartmentLabelList = opHouse.getApartmentLabelList();
        if (CollectionUtil.isNotEmpty(apartmentLabelList)) {
            String labelIds = StrUtil.join(",", apartmentLabelList.stream().map(ApartmentLabel::getLabelId).collect(Collectors.toList()));
            opHouse.setLabelIds(labelIds);
        }

        return opHouseMapper.insertOpHouse(opHouse);
    }

    /**
     * 修改公众号房源
     *
     * @param opHouse 公众号房源
     * @return 结果
     */
    @Override
    public int updateOpHouse(OpHouse opHouse) {
        opHouse.setUpdateTime(new Date());

        List<HouseFacilities> facilitiesList = opHouse.getFacilitiesList();
        if (CollectionUtil.isNotEmpty(facilitiesList)) {
            String facilitiesIds = StrUtil.join(",", facilitiesList.stream().map(HouseFacilities::getFacilitiesId).collect(Collectors.toList()));
            opHouse.setFacilitiesIds(facilitiesIds);
        }

        List<ApartmentLabel> apartmentLabelList = opHouse.getApartmentLabelList();
        if (CollectionUtil.isNotEmpty(apartmentLabelList)) {
            String labelIds = StrUtil.join(",", apartmentLabelList.stream().map(ApartmentLabel::getLabelId).collect(Collectors.toList()));
            opHouse.setLabelIds(labelIds);
        }
        return opHouseMapper.updateOpHouse(opHouse);
    }

    /**
     * 批量删除公众号房源
     *
     * @param ids 需要删除的公众号房源主键
     * @return 结果
     */
    @Override
    public int deleteOpHouseByIds(Long[] ids) {
        return opHouseMapper.deleteOpHouseByIds(ids);
    }

    /**
     * 删除公众号房源信息
     *
     * @param id 公众号房源主键
     * @return 结果
     */
    @Override
    public int deleteOpHouseById(Long id) {
        return opHouseMapper.deleteOpHouseById(id);
    }

    @Override
    public int updateOpHouseStatus(Long id, int status) {
        return opHouseMapper.updateOpHouseStatus(id, status);
    }

    @Override
    public int updateOpHouseStatusBatch(Long[] ids, int status) {
        return opHouseMapper.updateOpHouseStatusBatch(ids, status);
    }
}
